﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sunny.UI;

namespace MMS
{
    public partial class Page_SelectGoods : UIPage
    {
        public Page_SelectGoods()
        {
            InitializeComponent();
        }
        public bool root { get; set; }
        db_mmsEntities conn = new db_mmsEntities();
        int minNum=0;
        int maxNum;
        int clickIndex;
        private void Page_SelectGoods_Load(object sender, EventArgs e)
        {
            inIt();
            if (!root)
            {
                tabpan1.RowStyles[1] = new RowStyle(SizeType.Percent, 80);
                tabpan1.RowStyles[2] = new RowStyle(SizeType.Percent, 0);
            }
        }

        private void btn_Select_Click(object sender, EventArgs e)
        {
            
            uiDataGridView2.Rows.Clear();
            string gosid = txt_GosNb.Text;
            string gosname = txt_GosNm.Text;
            string gostype =com_GosType.Text;
            if (txt_Min.Text != "")
            {
                minNum = Convert.ToInt32(txt_Min.Text);
            }
            if (txt_Max.Text != "")
            {
                maxNum = Convert.ToInt32(txt_Max.Text);
            }
            var gos = conn.tb_goods.Where(r => r.goodsID.Contains(gosid) && r.goodsName.Contains(gosname) && r.goodsType.Contains(gostype) && r.goodsSum >= minNum && r.goodsSum <= maxNum&&r.isShow==1).ToList();
        
            foreach(var i in gos)
            {
                string[] temp = new string[] { i.goodsID, i.goodsName, i.goodsSum.ToString(), i.goodsParam, i.goodsUnits, i.goodsType, i.putPrice, i.getPrice };
                uiDataGridView2.Rows.Add(temp);
            }
            reSet();
        }

        private void inIt()
        {
            List<string> ty = new List<string>();
            uiDataGridView2.SetRowHeight(30);
            //初始化商品类型
            var gosType = conn.tb_goods.GroupBy(r => r.goodsType).Select(k=>k.Key ).ToList();
            foreach(var i in gosType)
            {
                ty.Add(i);
                
                //Console.WriteLine(i);
            }
            com_GosType.DataSource = ty;
            com_GetGosType.DataSource = ty;
            //获取最大商品数量
            maxNum = Convert.ToInt32(conn.tb_goods.Select(r => r.goodsSum).Max());
            txt_Min.Watermark = minNum.ToString();
            txt_Max.Watermark = maxNum.ToString();
            lab_Tips.Text = "可查询数量范围" + minNum + "~" + maxNum;


            
        }

        private void reSet()
        {
            
            txt_GosNb.Clear();
            txt_GosNm.Clear();
            txt_Max.Clear();
            txt_Min.Clear();
            com_GosType.Text = "";

            txt_GetGosNm.Clear();
            txt_GetGosNb.Clear();
            txt_GetGosPar.Clear();
            txt_GetGosUni.Clear();
            com_GetGosType.Text = "";
            txt_GetGosGet.Clear();
            txt_GetGosPut.Clear();
        }

        private void uiDataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            clickIndex = e.RowIndex;
            //MessageBox.Show(index.ToString());
            //所在行商品编号
            txt_GetGosID.Text = uiDataGridView2.Rows[clickIndex].Cells[0].Value.ToString();
            txt_GetGosNm.Text = uiDataGridView2.Rows[clickIndex].Cells[1].Value.ToString();
            txt_GetGosNb.Text = uiDataGridView2.Rows[clickIndex].Cells[2].Value.ToString();
            txt_GetGosPar.Text =uiDataGridView2.Rows[clickIndex].Cells[3].Value.ToString();
            txt_GetGosUni.Text =uiDataGridView2.Rows[clickIndex].Cells[4].Value.ToString();
            com_GetGosType.Text=uiDataGridView2.Rows[clickIndex].Cells[5].Value.ToString();
            txt_GetGosGet.Text =uiDataGridView2.Rows[clickIndex].Cells[6].Value.ToString();    
            txt_GetGosPut.Text =uiDataGridView2.Rows[clickIndex].Cells[7].Value.ToString();
            
        }

        /// <summary>
        /// 删除商品
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_remove_Click(object sender, EventArgs e)
        {
            string getgosid = txt_GetGosID.Text;
            if (getgosid == "")
            {
                MessageBox.Show("请先选中要删除的商品");
                return;
            }
            tb_goods g = conn.tb_goods.FirstOrDefault(r => r.goodsID == getgosid);
            g.isShow = 0;
            conn.SaveChanges();
            uiDataGridView2.Rows.RemoveAt(clickIndex);
            Init();
        }

        private void btn_Reset_Click(object sender, EventArgs e)
        {
            string getgosid = txt_GetGosID.Text;
            if (getgosid == "")
            {
                MessageBox.Show("请先选中要修改的商品");
                return;
            }
            tb_goods g = conn.tb_goods.FirstOrDefault(r => r.goodsID == getgosid);
            g.goodsName= txt_GetGosNm.Text;
            g.goodsSum = Convert.ToInt32(txt_GetGosNb.Text);
            g.goodsParam= txt_GetGosPar.Text;
            g.goodsUnits= txt_GetGosUni.Text;
            g.goodsType= com_GetGosType.Text;
            g.getPrice  = txt_GetGosGet.Text;
            g.putPrice = txt_GetGosPut.Text;
            conn.SaveChanges();
            uiDataGridView2.Rows[clickIndex].Cells[1].Value = txt_GetGosNm.Text;
            uiDataGridView2.Rows[clickIndex].Cells[2].Value = txt_GetGosNb.Text;
            uiDataGridView2.Rows[clickIndex].Cells[3].Value = txt_GetGosPar.Text;
            uiDataGridView2.Rows[clickIndex].Cells[4].Value = txt_GetGosUni.Text;
            uiDataGridView2.Rows[clickIndex].Cells[5].Value = com_GetGosType.Text;
            uiDataGridView2.Rows[clickIndex].Cells[6].Value = txt_GetGosGet.Text;
            uiDataGridView2.Rows[clickIndex].Cells[7].Value = txt_GetGosPut.Text;
            Init();
        }
    }
}
